﻿@page "/InboundPage"
@inject PickingServer pickingServer
@inject EmptyCache_EntityServer emptyCache_EntityServer
@using System.ComponentModel
@using AntDesign.TableModels
@using ShanjinPDAUI.ModelEntity
@inject ModalService _modalService
@inject MessageService _message
<h3>下料</h3>
<div>
    <Row>
        <Col Span="24">请选择主机</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@Machines"
                AllowClear="true"
                ValueOnClear="@(0)"
                @bind-Value="@Machineselet"
                ValueName="@nameof(Machine_Entity.Devid)"
                LabelName="@nameof(Machine_Entity.Remark)">
        </Select>
        </Col>
    </Row>
    <Row>
        <Col Span="24">
        <label>宽幅（主机退料需要填写）</label>
        </Col>
    </Row>
    <Row>
        <Col Span="24">
        <Input style="width:100%" Placeholder="Basic usage" @bind-Value="@width" />
        </Col>
    </Row>
    <Row>
        <Col Span="24">
        <label>是否直接出库</label>
        </Col>
    </Row>
    <Row>
        <Col Span="8" Push="8">
        <Switch style="width:100%" @bind-Value="@IsDirectOutBuond"></Switch>
        </Col>
    </Row>
    <Row>
        <Col Span="24">
        <label>是否大卷（主机退料需要选择）</label>
        </Col>
    </Row>
    <Row>
        <Col Span="8" Push="8">
        <Switch style="width:100%" @bind-Value="@Flag"></Switch>
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
        <label>是否出空卷芯（仅限涂层下料）</label>
        </Col>
    </Row>
    <Row>
        <Col Span="8" Push="8">
        <Switch style="width:100%" @bind-Value="@IsOutBoundEmpty"></Switch>
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">主机下料</Button>
        </Col>
    </Row>
    <br>
</div>

@code {
    ObservableCollection<Machine_Entity> Machines { get; set; } = new ObservableCollection<Machine_Entity>();
    ObservableCollection<FullCache_Entity> FullCaches { get; set; } = new ObservableCollection<FullCache_Entity>();
    int Machineselet { get; set; } = 0;
    bool Flag { get; set; } = false;
    bool IsOutBoundEmpty { get; set; } = false;
    int width;
    int chaneselet { get; set; } = 0;
    bool IsDirectOutBuond = false;
    protected override async Task OnInitializedAsync()
    {
        var machines = await pickingServer.GetMachine();
        if (machines != null)
        {
            Machines.Clear();
            foreach (var item in machines)
            {
                Machines.Add(item);
            }
        }

        base.OnInitialized();
    }
    private async Task Creat()
    {
        _modalService.Confirm(new ConfirmOptions()
            {
                Title = "提示",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            Content = "是否提交？",
            OkText = "确认",
            OnOk = async (e) =>
            {
                var Machine = Machines.FirstOrDefault(x => x.Devid == Machineselet);
                if (Machine != null)
                {
                    if (Machine.IsLk)
                    {
                        string inBoundresult = "";
                        string outBoundresult = "";
                        int type=0;
                        //入库
                        inBoundresult = await pickingServer.Inbound(Machine, Flag, IsDirectOutBuond);
                        if (inBoundresult.Contains("OK") && IsOutBoundEmpty && (Machine.MESName.Contains("K3PCOT001_18") || Machine.MESName.Contains("K3PCOT002_18")))
                        {
                            if (Machine.MESName.Contains("K3PCOT001_18"))
                            {
                                type = 1;
                            }
                            else if (Machine.MESName.Contains("K3PCOT002_18"))
                            {
                                type = 2;
                            }
                            var emptyCahe = await emptyCache_EntityServer.GetEmptyCache(type);
                            if (emptyCahe==null)
                            {
                                await _message.Info($"下料任务：{inBoundresult}");
                                await _message.Info($"取空卷芯任务：未找到有空卷芯的缓存架");
                                return;
                            }
                            outBoundresult = await pickingServer.EmptyPICK(Machine, Flag, emptyCahe.Deviceid);
                            await _message.Info($"下料任务：{inBoundresult}");
                            await _message.Info($"取空卷芯任务：{outBoundresult}");
                        }
                        else
                        {
                            await _message.Info(inBoundresult);
                        }
                        
                    }
                    else
                    {
                        
                  
                    }
                }

            },
            CancelText = "取消"
        });


    }
}

